package org.jumiz.dbview.core.service;

import org.jumiz.dbview.own.repository.ConnectRepository;
import org.jumiz.dbview.own.service.DatabasesServiceFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TableService {

    @Autowired
    private ConnectRepository connectRepository;

    public Mono<HashMap<String, Object>> load(Long id, String table) {
        return connectRepository.findById(id).map(item -> {
            List<String> columns = DatabasesServiceFactory.getBean(item.getDbType()).getColumns(item, null, table);
            List<Map<String, Object>> tableData = DatabasesServiceFactory.getBean(item.getDbType()).getTableData(item, table, columns);
            HashMap<String, Object> result = new HashMap<>();
            result.put("tableColumns", columns);
            result.put("tableData", tableData);
            return result;
        });
    }
}
